/**
 * 
 */
package com.epam.training.service.paymant;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.epam.training.domain.Paymant;
import com.epam.training.repository.PaymantRepository;

/**
 * @author Bogdan
 *
 */
@Service
public class PaymantServiceImpl implements PaymantService {

	@Autowired
	PaymantRepository repository;

	private final static BigDecimal PRICE_KOEFICICENT = new BigDecimal("1.4");
	public final static Integer TIME_TO_DAY = 1000 * 60 * 60 * 24;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.epam.training.service.paymant.PaymantService#getById(java.lang.Integer
	 * )
	 */
	@Override
	public Paymant getById(Integer id) {
		return repository.findOne(id);
	}

	@Transactional
	@Override
	public Paymant save(Paymant paymant) {
		return repository.save(paymant);
	}

	@Override
	public void delete(Integer id) {
		repository.delete(id);
	}

	@Override
	public BigDecimal calculatePrice(Date day1, Date day2, BigDecimal carPrice,
			BigDecimal insurancePrice) {
		long date = Math.abs(day1.getTime() - day2.getTime()) / TIME_TO_DAY;

		BigDecimal price = carPrice.add(insurancePrice);

		if (date < 1) {
			return price;
		} else {
			return price = price.multiply(PRICE_KOEFICICENT);
		}
	}

}
